package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.FormatAdapter;
import flc.ast.bean.FormatBean;
import flc.ast.databinding.DialogFormatStyleBinding;
import stark.common.basic.base.BaseSmartDialog;

public class FormatDialog extends BaseSmartDialog<DialogFormatStyleBinding> {
    private List<FormatBean> mFormatBeans = new ArrayList<>();
    private FormatAdapter mFormatAdapter;
    private int mCurrentPos = 1;
    private int type;


    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    public void setType(int type) {
        this.type = type;
    }

    public FormatDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_format_style;
    }

    @Override
    protected void initView(View view) {
        mFormatAdapter = new FormatAdapter();
        mDataBinding.rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataBinding.rvList.setAdapter(mFormatAdapter);
        getDialogData();
        mFormatAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                mFormatAdapter.getItem(mCurrentPos).setSelect(false);
                mFormatAdapter.getItem(position).setSelect(true);
                mFormatAdapter.notifyDataSetChanged();
                mCurrentPos = position;
                if (listener != null) {
                    listener.clickItemName(mFormatAdapter.getItem(mCurrentPos).getName());
                }
                dismiss();
            }
        });
    }


    private void getDialogData() {
        switch (type) {
            case 0:
                getTargetData();
                break;
            case 1:
                getResolution();
                break;
            case 2:
                getCodeData();
                break;
            case 3:
                getFrameData();
                break;
            default:
                break;
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }


    public interface IListener {
        //点击item
        void clickItemName(String name);
    }

    private void getFrameData() {
        mFormatBeans.add(new FormatBean("5fps", false));
        mFormatBeans.add(new FormatBean("10fps", false));
        mFormatBeans.add(new FormatBean("15fps", false));
        mFormatBeans.add(new FormatBean("20fps", false));
        mFormatBeans.add(new FormatBean("25fps", false));
        mFormatBeans.add(new FormatBean("30fps", true));
        mFormatAdapter.setList(mFormatBeans);
    }

    private void getCodeData() {
        mFormatBeans.add(new FormatBean("250kb/s", false));
        mFormatBeans.add(new FormatBean("1000kb/s", false));
        mFormatBeans.add(new FormatBean("1500kb/s", false));
        mFormatBeans.add(new FormatBean("2000kb/s", true));
        mFormatBeans.add(new FormatBean("3000kb/s", false));
        mFormatAdapter.setList(mFormatBeans);
    }

    private void getResolution() {
        mFormatBeans.add(new FormatBean("2k", false));
        mFormatBeans.add(new FormatBean("1080p", true));
        mFormatBeans.add(new FormatBean("720p", false));
        mFormatBeans.add(new FormatBean("540p", false));
        mFormatBeans.add(new FormatBean("480p", false));
        mFormatAdapter.setList(mFormatBeans);
    }

    private void getTargetData() {
        mFormatBeans.add(new FormatBean("mp4", false));
        mFormatBeans.add(new FormatBean("avi", false));
        mFormatBeans.add(new FormatBean("mkv", false));
        mFormatBeans.add(new FormatBean("mov", false));
        mFormatBeans.add(new FormatBean("3gp", false));
        mFormatAdapter.setList(mFormatBeans);
    }
}
